//
//  JCNSTableView.swift
//  JCTest3
//
//  Created by Chen on 10/10/2022.
//

import Cocoa

class JCNSTableView: JCBaseBottomView {
    
    private var simulates = ["iOS 14.13", "iOS 14.12", "iOS 14.0", "iOS 13.7", "iOS 13.6", "iOS 13.5", "iOS 13.4", "iOS 13.2", "iOS 13.0", "iOS 13.0", "iOS 12.4", "iOS 12.2"]
    private var fileSizes = ["3.33 GB", "3.17 GB", "3.33 GB", "2.38 GB", "2.57 GB", "2.49 GB", "2.4 GB", "2.14 GB", "2.14 GB", "2.09 GB", "2.18 GB", "1.59 GB", ]
    // 懒加载有问题
    private var scrollViewLeft: NSScrollView?
    private var scrollViewRight: NSScrollView?
    private var tableViewLeft: NSTableView = NSTableView()
    private var tableViewRight: NSTableView = NSTableView()
    
    /* 两个TableView时，懒加载有问题。
    private lazy var scrollView1: NSScrollView = {
        let scrollView = NSScrollView()
        scrollView.hasVerticalScroller = true
        scrollView.autoresizingMask = [.width, .height]
        return scrollView
    }()
    
    private lazy var tableViewLeft: NSTableView = {
        let tableView = NSTableView.init(frame: NSRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        // NSTableView需要初始化至少一个colume，默认是0个
        let firstColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MyFirstColume"))
        firstColumn.title = "First"
        tableView.addTableColumn(firstColumn)
        let secondColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MySecondColume"))
        secondColumn.title = "Second"
        tableView.addTableColumn(secondColumn)
        
        let thirdColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MyThirdColumn"))
        tableView.addTableColumn(thirdColumn)
        thirdColumn.title = "Third"
                
        return tableView
    }()
    
    private lazy var scrollView2: NSScrollView = {
        let scrollView = NSScrollView()
        scrollView.hasVerticalScroller = true
        scrollView.autoresizingMask = [.width, .height]
        return scrollView
    }()
    
    private lazy var tableViewRight: NSTableView = {
        let tableView = NSTableView(frame: NSRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        // 是否允许column重新排序，默认true
        tableView.allowsColumnReordering = false
        // 是否允许column调节宽度，默认true
        tableView.allowsColumnResizing = true
        // 是否允许选中整列，点击列头部选中整列，默认true
        tableView.allowsColumnSelection = true
        // 是否可以斑马线样式，默认false
        tableView.usesAlternatingRowBackgroundColors = true
        // NSTableView需要初始化至少一个colume，默认是0个
        let firstColume = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "IdSimulateColume"))
        firstColume.title = "Simulate"
        tableView.addTableColumn(firstColume)
        let secondColume = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "IdSizeColume"))
        secondColume.title = "Size"
        secondColume.headerToolTip = "size多大"
        tableView.addTableColumn(secondColume)
        
        return tableView
    }()
     */

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        self.setupViewBackgroundColor(color: NSColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0))
        // Drawing code here.
        self.ccreateSubviews()
        
    }
    
}

// MARK: - 加载视图
extension JCNSTableView {
    
    func ccreateSubviews() {
        
        self.scrollViewLeft = NSScrollView(frame: NSRect(x: 24, y: 24, width: 300, height: 300))
        self.addSubview(self.scrollViewLeft!)
        
        self.tableViewLeft.frame = NSRect(x: 0, y: 0, width: 300, height: 300)
        self.tableViewLeft.delegate = self
        self.tableViewLeft.dataSource = self
        
        // NSTableView需要初始化至少一个colume，默认是0个
        let firstColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MyFirstColume"))
        firstColumn.title = "First"
        self.tableViewLeft.addTableColumn(firstColumn)
        let secondColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MySecondColume"))
        secondColumn.title = "Second"
        self.tableViewLeft.addTableColumn(secondColumn)
        
        let thirdColumn = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "MyThirdColumn"))
        self.tableViewLeft.addTableColumn(thirdColumn)
        thirdColumn.title = "Third"
        
        self.scrollViewLeft?.documentView = self.tableViewLeft
        
        self.scrollViewRight = NSScrollView(frame: NSRect(x: 348, y: 24, width: 300, height: 300))
        self.addSubview(self.scrollViewRight!)
        
        self.tableViewRight.frame = NSRect(x: 0, y: 0, width: 300, height: 300)
        self.tableViewRight.delegate = self
        self.tableViewRight.dataSource = self
        // 是否允许column重新排序，默认true
        self.tableViewRight.allowsColumnReordering = false
        // 是否允许column调节宽度，默认true
        self.tableViewRight.allowsColumnResizing = true
        // 是否允许选中整列，点击列头部选中整列，默认true
        self.tableViewRight.allowsColumnSelection = true
        // 是否可以斑马线样式，默认false
        self.tableViewRight.usesAlternatingRowBackgroundColors = true
        // NSTableView需要初始化至少一个colume，默认是0个
        let simulateColume = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "IdSimulateColume"))
        simulateColume.title = "Simulate"
        self.tableViewRight.addTableColumn(simulateColume)
        let sizeColume = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "IdSizeColume"))
        sizeColume.title = "Size"
        sizeColume.headerToolTip = "size多大"
        self.tableViewRight.addTableColumn(sizeColume)
        
        self.scrollViewRight?.documentView = self.tableViewRight
        
    }
    
}

// MARK: - 测试
extension JCNSTableView {
   
    // 测试autolayout加约束
    func textConstrains() {
        
        let view = NSView(frame: NSRect(x: 0, y: 0, width: 80, height: 40))
        view.setupViewBackgroundColor(color: .yellow)
        self.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        
//        self.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self.scrollView1, attribute: .bottom, multiplier: 1, constant: 30))
//        self.addConstraint(NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 24))
//        // multiplier 0 直接设置
//        self.addConstraint(NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: 200))
//        self.addConstraint(NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 40))
        
    }
    
}

// MARK: - NSTableViewDelegate
extension JCNSTableView: NSTableViewDelegate {}
// MARK: - NSTableViewDataSource
extension JCNSTableView: NSTableViewDataSource {

    func numberOfRows(in tableView: NSTableView) -> Int {
        return tableView.isEqual(self.tableViewLeft) ? 10 : self.simulates.count
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        return tableView.isEqual(self.tableViewLeft) ? 30 : 60
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        
        let text: NSTextField = NSTextField()
        
        if tableView.isEqual(self.tableViewLeft) {
            
            if tableColumn?.identifier == NSUserInterfaceItemIdentifier("MyFirstColume") {
                text.stringValue = "\(row)"
            } else {
                text.stringValue = "这是来自第\(row)行的问候"
            }
            
        } else {
            if tableColumn?.identifier == NSUserInterfaceItemIdentifier("Simulate") {
                text.stringValue = self.simulates[row]
            } else {
                text.stringValue = self.fileSizes[row]
            }
        }
        
        let cell = NSTableCellView()
        cell.addSubview(text)
        
        text.isEditable = false
        text.drawsBackground = false
        text.isBordered = false
        text.alignment = .left
        text.translatesAutoresizingMaskIntoConstraints = false
        
        return cell
    }
    
//    func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
//        let rowView = NSTableRowView()
//        rowView.isEmphasized = false
//        return rowView
//    }
    
    func tableView(_ tableView: NSTableView, toolTipFor cell: NSCell, rect: NSRectPointer, tableColumn: NSTableColumn?, row: Int, mouseLocation: NSPoint) -> String {
        return tableView == self.tableViewLeft ? "这是左边table的悬停文字" : "这是右边table的悬停文字"
    }
}
